﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
namespace HowItWentDown
{
    public static class Camera
    {
        /// <summary>
        /// The object that the camera will turn to face
        /// </summary>
        public static Obj LookAt;
        /// <summary>
        /// The object that the camera will move to follow
        /// </summary>
        public static Obj FollowObj;
        /// <summary>
        /// Field of view of the camera. 45 is the default. Increasing this will make things look farther away, while decreasing it will make things look closer.
        /// </summary>
        public static float FieldOfView = 45;
        /// <summary>
        /// The position that the camera is currently at.
        /// </summary>
        public static Vector3 Pos = Vector3.Zero;
        /// <summary>
        /// The position of the that the camera is pointing towards
        /// </summary>
        public static Vector3 LookPos = Vector3.Zero;
        static Vector3 postarg = Vector3.Zero, lookpostarg = Vector3.Zero, postarg1 = Vector3.Zero, lookpostarg1 = Vector3.Zero;
        /// <summary>
        /// If the distance from the camera to the object is smaller than this number, then the camera will move away from the object.
        /// </summary>
        public static float MaXDistance = 100;
        /// <summary>
        /// The distance along the Z axis that the camera must be away from the object. This will help in keeping the camera at the same angle from the player.
        /// Set it to float.NaN to disable
        /// </summary>
        public static float ZDistance = 100;
        public static void Update()
        {
            if (LookAt != null)
            {
                //look at given position
                lookpostarg = LookAt.pos;
                lookpostarg1 += (lookpostarg - lookpostarg1) * 0.1f * Global.Speed;
                LookPos += (lookpostarg1 - LookPos) * 0.1f * Global.Speed;
            }
            if (FollowObj != null)
            {
                postarg = FollowObj.pos + (new Vector3(FollowObj.rect.Origin.X, FollowObj.rect.Origin.Y, 0) * FollowObj.scale);
                float dist = MyMath.Distance(postarg, postarg1);
                float mul = ((dist - MaXDistance) / (10));
                if (mul > 0.2f)
                    mul = 0.2f;
                else if (mul < -0.2f)
                    mul = -0.2f;
                else if (mul == float.NaN)
                    mul = 0.1f;
                postarg1 += (postarg - postarg1) * mul * Global.Speed;
                if (postarg1.Y < FollowObj.y + 30)
                    postarg1.Y = FollowObj.y + 30;
                if (!float.IsNaN(ZDistance))
                {
                    if (postarg1.Z > postarg.Z - ZDistance)
                    {
                        postarg1.Z = postarg.Z - ZDistance;
                    }
                }
                Pos += (postarg1 - Pos) * 0.04f * Global.Speed;
                //Camera must follow player, but remain at the same angle in relation to him.
            }
        }
    }
}
